package com.gehj.base.broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;


/*全局检测网络状态变化简单进行提示*/
public class NetChangeReceiver extends BroadcastReceiver {
  //  public static final  String ACTION = "com.gasplatform.ygsj.mashgasmonitoring.broadcast.NetChangeReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Boolean isConnect = isNetworkAvailable(context);
        if (!isConnect){
            Toast.makeText(context, "网络中断,请检查网络设置", Toast.LENGTH_SHORT).show();
        }
        //throw new UnsupportedOperationException("Not yet implemented");

    }

    /**
     * 检查是否连接到了网络
     */
    public static  boolean isNetworkAvailable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {// 当前网络是连接的
            if (info.getState() == NetworkInfo.State.CONNECTED) { // 当前所连接的网络可用
                return true;
            }
        }
        return false;
    }


}
